<template>
  <div style="width: 100%" v-if="visible">
    <el-form ref="form" :model="form" :rules="rules" label-position="top" size="mini">
      <el-row>
        <el-col :span="24">
          <el-form-item label="提交审批" prop="approval">
            <el-select v-model="form.approval" placeholder="请选择审批方式" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in approvalList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述" prop="description">
            <el-input type="textarea" :rows="3" size="small" v-model="form.description"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" style="width: 100%" type="primary" @click="submitForm">保 存</el-button>
    </div>
  </div>
</template>

<script>
  import {nodeInfo, updateInfo, addNodeInfo} from '@/lib/api-pipeline';

  import Vue from 'vue'
  import Element from 'element-ui'
  import 'element-ui/lib/theme-chalk/index.css';

  Vue.use(Element)

	export default {
		name: "manual-clamping-point",

    data() {
      return {
        id:0,
        visible: false,
        nodeUuid: '',
        form: {
          approval: '',
          description: '',
        },
        approvalList: [
          {
            value: 'backstageApproval',
            label: '后台审批'
          },
          {
            value: 'approvers',
            label: '审批人员'
          }
        ],
        rules: {
          approval: [{required: true, message: '审批方式不能为空', trigger: 'blur'}],
        }
      }
    },

    methods: {
      init(nodeUuid) {
        this.nodeUuid = nodeUuid
        nodeInfo({'nodeUuid': nodeUuid}).then(response => {
          if (response.code === 200) {
            const data = response.data
            if (data) {
              this.form = JSON.parse(data.info)
              this.id = data.id
            }
          } else {
            this.msgError(response.msg)
          }
          this.visible = true
        })
      },

      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            // 构建数据json
            let info = {
              'id':this.id,
              'nodeUuid': this.nodeUuid,
              'nodeName': '人工卡点',
              'info': JSON.stringify(this.form)
            }
            if (this.id !== 0) {
              updateInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addNodeInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.id = response.data //更新新增节点id
                  this.msgSuccess('新增成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
    }

	}
</script>

<style scoped>

</style>
